package apis

import (
	"fmt"
	"vrcm/src/vrcm/app/admin/models/live"
	"vrcm/src/vrcm/common/apis"

	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
	"vrcm/src/vrcm/app/admin/service/dto/live"
	"vrcm/src/vrcm/app/admin/service/live"
	"vrcm/src/vrcm/common/actions"
)

type RecordListView struct {
	api.Api
}

// GetPage 获取RecordListView列表
// @Summary 获取RecordListView列表
// @Description 获取RecordListView列表
// @Tags RecordListView
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.RecordListView}} "{"code": 200, "data": [...]}"
// @Router /api/v1/record-list-view [get]
// @Security Bearer
func (e RecordListView) GetPage(c *gin.Context) {
	req := dto.RecordListViewGetPageReq{}
	s := service.RecordListView{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	list := make([]live.RecordListView, 0)
	var count int64

	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取RecordListView失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

func (e RecordListView) GetPageByTime(c *gin.Context) {
	req := dto.RecordListViewGetPageReq{}
	s := service.RecordListView{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	//list := make([]live.RecordListView, 0)
	var count int64

	model := live.RecordListViewByTime{}
	err = s.GetPageByTime(&req, &model, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取RecordListView失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(model, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

func (e RecordListView) GetPageRecordList(c *gin.Context) {
	req := dto.RecordListGetPageReq{}
	s := service.RecordListView{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	req.TenantId = c.Query("permissionTenantId")
	list := make([]live.RecordList, 0)
	var count int64

	err = s.GetPageRecordList(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取RecordList失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

func (e RecordListView) GetPageNeedRecordList(c *gin.Context) {
	req := dto.RecordListGetPageReq{}
	s := service.RecordListView{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	req.TenantId = c.Query("permissionTenantId")
	list := make([]live.RecordList, 0)
	var count int64

	err = s.GetPageNeedRecordList(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取RecordList失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取RecordListView
// @Summary 获取RecordListView
// @Description 获取RecordListView
// @Tags RecordListView
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.RecordListView} "{"code": 200, "data": [...]}"
// @Router /api/v1/record-list-view/{id} [get]
// @Security Bearer
func (e RecordListView) Get(c *gin.Context) {
	req := dto.RecordListViewGetReq{}
	s := service.RecordListView{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	var object live.RecordListView

	p := actions.GetPermissionFromContext(c)
	err = s.Get(&req, p, &object)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取RecordListView失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}

// Insert 创建RecordListView
// @Summary 创建RecordListView
// @Description 创建RecordListView
// @Tags RecordListView
// @Accept application/json
// @Product application/json
// @Param data body dto.RecordListViewInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/record-list-view [post]
// @Security Bearer
func (e RecordListView) Insert(c *gin.Context) {
	req := dto.RecordListViewMultiInsertReq{}
	s := service.RecordListView{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	if len(req.RecordViews) <= 0 {
		e.OK("", "创建成功")
		return
	}
	if len(req.RecordViews) <= 0 {
		e.Error(apis.InternalError.Code, err, "RecordViews 为空")
		return
	}
	count, err := s.GetCount(req.RecordViews[0].BasicId)
	req.Count = count
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.DBGetCountError.Code, err, err.Error())
		return
	}
	err = s.Insert(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("创建RecordListView失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK("", "创建成功")
}

// Update 修改RecordListView
// @Summary 修改RecordListView
// @Description 修改RecordListView
// @Tags RecordListView
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.RecordListViewUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/record-list-view/{id} [put]
// @Security Bearer
func (e RecordListView) Update(c *gin.Context) {
	req := dto.RecordListViewUpdateReq{}
	s := service.RecordListView{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	err = s.Update(&req)
	if err != nil {
		if err.Error() == string(apis.ImageError.Code) {
			e.Error(apis.ImageError.Code, err, "修改RecordListView失败，失败信息: 图片格式不正确")
		} else {
			e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改RecordListView失败，\r\n失败信息 %s", err.Error()))
		}
		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除RecordListView
// @Summary 删除RecordListView
// @Description 删除RecordListView
// @Tags RecordListView
// @Param data body dto.RecordListViewDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/record-list-view [delete]
// @Security Bearer
func (e RecordListView) Delete(c *gin.Context) {
	s := service.RecordListView{}
	req := dto.RecordListViewDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Remove(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("删除RecordListView失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

func (e RecordListView) DeleteRecord(c *gin.Context) {
	s := service.RecordListView{}
	req := dto.RecordListViewDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.RemoveRecord(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("删除RecordListView失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

// SyncRecord 同步录制文件
func (e RecordListView) SyncRecord(c *gin.Context) {
	req := dto.SyncRecordReq{}
	s := service.RecordListView{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.SyncRecord(&req)
	if err != nil {
		e.Error(apis.SyncRecordError.Code, err, fmt.Sprintf("同步RecordList失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK("", "同步录制视频成功")
}

// GetNeedToAddRecords  获取需要添加录制文件的列表
func (e RecordListView) GetNeedToAddRecords(c *gin.Context) {
	req := dto.GetNeedToAddRecordsReq{}
	s := service.RecordListView{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	var count int64

	data, err := s.GetNeedToAddRecords(&req, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取RecordListView失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(data, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// RecordUpOrDown  上移下移
func (e RecordListView) RecordUpOrDown(c *gin.Context) {
	req := dto.RecordUpOrDownReq{}
	s := service.RecordListView{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.RecordUpOrDown(&req)
	if err != nil {
		e.Error(apis.UpOrDownError.Code, err, fmt.Sprintf("获取RecordListView失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK("", "上移或下移成功")
}

func (e RecordListView) GetWs(c *gin.Context) {
	req := dto.RecordUpOrDownReq{}
	s := service.RecordListView{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.RecordUpOrDown(&req)
	if err != nil {
		e.Error(apis.UpOrDownError.Code, err, fmt.Sprintf("获取RecordListView失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK("", "上移或下移成功")
}
